package mmc.objects;

import static org.lwjgl.opengl.GL11.*;
import static mmc.resource.Sprite2D.*;
import static mmc.util.Utils.wrap4f;

import mmc.Client;
import mmc.EngineObject;
import mmc.Light;
import mmc.Player;
import mmc.Scene;

@SuppressWarnings("serial")
public class Sun extends Light{

	private float dayTime; //0 = 00:00, 1 = 24:00
	private float secondsPerDay=30*60; //30 minutes
	private static final float radius = 1e4f;
	
	private static final float[] RGB_SKY_DAY={51/256f,120/256f,190/256f};
	
	public Sun() {
		super(0,radius,0);
	}
	
	public void initOnClient(Scene scene){
		glLight(lightId, GL_AMBIENT, wrap4f(2,2,2,2));
		glLight(lightId, GL_DIFFUSE, wrap4f(1,1,1,1));
		glLight(lightId, GL_SPECULAR, wrap4f(1,1,1,1));
		glLight(lightId, GL_POSITION, wrap4f(0,0,0,0));
		
		glEnable(lightId);
	}
	
	@Override
	public boolean inRange(EngineObject other, float range) {
		return true; //everyone is allowed to see the sun (viewrange doesn't matter)
	}
	
	@Override
	public void render(EngineObject observer) {
		Player p = Client.client.getScene().player;
		
		glColor4f(1,1,1,1);
		glEnable(GL_TEXTURE);
		glSprite3D2f("mmc/resource/data/Sun2.png",p.x+x,p.y + y,p.z,100);
	}
	
	@Override
	public void update(float dt) {
		dayTime += dt/secondsPerDay;
		
		x = (float) (Math.sin(dayTime*2*Math.PI)*radius);
		y = (float) (Math.cos(dayTime*2*Math.PI)*radius);
		
		float f = ((float) Math.cos(dayTime*2*Math.PI)+1)/2;
		glClearColor(f*RGB_SKY_DAY[0], f*RGB_SKY_DAY[1], f*RGB_SKY_DAY[2], 0f);
	}
	
	public boolean copyTo(EngineObject o){
		copyFieldsTo((Sun)o);
		((Sun)o).dayTime = dayTime;
		((Sun)o).secondsPerDay = secondsPerDay;
		
		return true;
	}
	
	public void reset(){
		dayTime = 0;
		dirty=true;
	}
	
	public void setSecondsPerDay(float s){
		secondsPerDay = s;
		dirty=true;
	}
}
